package com.itkaien.javaeebased.multipleSystem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.concurrent.Callable;
import java.util.concurrent.locks.ReentrantLock;

public class work7CallAble implements Callable<Integer> {

    ArrayList<Integer> list;
    ReentrantLock reentrantLock = new ReentrantLock();

    public work7CallAble(ArrayList<Integer> list) {
        this.list = list;
    }

    public work7CallAble() {
    }

    @Override
    public Integer call() throws Exception {
        ArrayList boxList=new ArrayList<Integer>();

        while(true){
            try {
                reentrantLock.lock();
                if (list.size()==0){
                    System.out.println("Thread "+Thread.currentThread().getName()+" boxList:"+boxList);
                    break;
                } else {
                    Collections.shuffle(list);
                    int price=list.remove(0);
                    boxList.add(price);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }finally {
                reentrantLock.unlock();
            }
        }

        boxList.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1-o2;
            }
        });

        System.out.println(boxList);
        return (Integer) Collections.max(boxList);
    }
}
